//
//  strategyView.m
//  HappilyLoan
//
//  Created by 纵情向前 on 2017/4/23.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "strategyView.h"
#import "PublicClass.h"
#import "UIImageView+WebCache.h"
#import "strategyViewController.h"
#import "topStrategyViewCell.h"
#import "MJRefresh.h"
#import "StrategyModel.h"
#import "ZFNewsRequestModel.h"

@interface strategyView()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property(nonatomic,strong)NSMutableArray *todataArray;//数据
@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation strategyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [ZFNewsRequestModel requestFindActivityType:@"1" CurrentPage:@"1" SucessBlock:^(NSDictionary *dict) {
            _todataArray = [NSMutableArray array];

            NSLog(@"ooo:%@",dict);
            NSArray *array = dict[@"data"][@"list"];
            NSMutableArray *infoArray = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                StrategyModel *cellInfo = [StrategyModel getStrategyInfoDictionary:dic];
                [infoArray addObject:cellInfo];
            }
            self.todataArray = infoArray;
            
        } failureBlock:^(NSInteger failSign) {
            
        }];
    }
    return self;
}
- (void)action_toStrategyNotification:(NSNotification *)sender {
    //数据处理
    //    NSLog(@"攻略的数据：%@",sender.object);
    
    
    
    //    _dataArray = [NSMutableArray array];
    //    if ([sender.object[@"is_alert"] integerValue] == 1) {
    //    }
    //
    //    if ([sender.object[@"code"] integerValue] == 1) {
    //
    //        for (NSDictionary *dic in sender.object[@"data"][@"list"]) {
    //            StrategyModel *strInfo = [StrategyModel getStrategyInfoDictionary:dic];
    //            NSLog(@"info:%@",strInfo);
    //            [_dataArray addObject:strInfo];
    //        }
    //
    //    }
    
    //        NSLog(@"攻略：HHH%@",_dataArray);
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.todataArray.count;
    return 5;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;//几个分组
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    topStrategyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor  = [UIColor clearColor];
    
NSLog(@"的个数：%@",self.todataArray);
    StrategyModel *model = _todataArray[indexPath.row];
    cell.typeLabel.text = model.activity_title;
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.typeLabel.text = model.create_time;;
    NSURL *url = [NSURL URLWithString:model.activity_pic];
    [cell.phoneImage sd_setImageWithURL:url];

    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(ADAPTATION_WIDTH(80), ADAPTATION_HEIGHT(110));
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"哎呀，点我啦");
}
//设置当前分组之间的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(ADAPTATION_HEIGHT(25), ADAPTATION_WIDTH(5), ADAPTATION_HEIGHT(5), ADAPTATION_WIDTH(5));
}

#pragma mark -- getter/setter
-(UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen.width, ADAPTATION_HEIGHT(413))];
        _topView.backgroundColor = GetColor(246, 247, 248, 1);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];//
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;//定义纵向滑动
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, ADAPTATION_HEIGHT(5), 0);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, ADAPTATION_HEIGHT(15), MAINSCREEN_WIDTH, ADAPTATION_HEIGHT(383)) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[topStrategyViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHead"];
//        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(action_Reload)];
        [_topView addSubview:_collectionView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.frame = CGRectMake(ADAPTATION_WIDTH(10), ADAPTATION_HEIGHT(10), ADAPTATION_WIDTH(70), ADAPTATION_HEIGHT(14));
        titleLabel.text = @"最新攻略";
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentRight;
        titleLabel.font = FONT(14);
        titleLabel.textColor = [UIColor blackColor];
        [_collectionView addSubview:titleLabel];

    }
    return _topView;
}
#pragma mark - 刷新
- (void)action_Reload {
    //刷新
    [self.collectionView.mj_header beginRefreshing];
//    [[MBLoadHUD getInitWithMB] start];
    [self performSelector:@selector(endRefresh) withObject:nil afterDelay:1.1];
}
- (void)endRefresh {
//    [[MBLoadHUD getInitWithMB] stop];
//    [self.alertBox appearAndDisappearAlertWithString:@"暂无更多数据！"];
    [self.collectionView.mj_header endRefreshing];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
